package cn.nb12.xbox.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.nb12.xbox.po.Games;
import cn.nb12.xbox.po.Queryname;
import cn.nb12.xbox.respo.GenResult;
import cn.nb12.xbox.service.GamesService;
import cn.nb12.xbox.service.QuerynameService;
import cn.nb12.xbox.utils.GenerateBusinessCodeService;
import cn.nb12.xbox.utils.UpdateAllGame;

/**
 * 游戏
 * 
 * @author yh
 *
 */
@Controller
public class GamesController extends BaseController {

	@Autowired
	private GamesService gamesService;

	@Autowired
	private GenerateBusinessCodeService generateBusinessCodeService;

	@Autowired
	private UpdateAllGame updateAllGame;

	
	@Autowired
	private QuerynameService querynameService;

	/**
	 * 进入首页
	 * 
	 * @param view
	 * @return
	 */
	@RequestMapping("games_goMain")
	public ModelAndView goMain(ModelAndView view) {
		view.setViewName("pages/games/games_goMain");
		return view;
	}

	/**
	 * 查数据
	 * 
	 * @param gameName
	 * @return
	 */
	@RequestMapping("games_getMainData")
	public ModelAndView getMainData(ModelAndView view, String gameName) {
		try {
			if (gameName == null) {
				gameName = "";
			}
			final String gameNameStr = gameName.trim();
			if (gameName != null && gameName.trim().length() > 0) {
				try {
					Queryname queryname=new Queryname();
					queryname.setId(generateBusinessCodeService.doWork());
					queryname.setName(gameNameStr);
					querynameService.insertNonEmptyQueryname(queryname);
				} catch (Exception e) {
					log.info("用户查询时入库失败"+e.getMessage());
				}
				log.info("查询数据入库完毕");
			}
			List<Games> games = null;

			if (gameName != null && gameName.trim().length() > 0) {
				games = gamesService.selectGamesByName(gameNameStr);
				view.getModel().put("data", games);
			}

			// 查询数据库

			view.setViewName("pages/games/games_main");
			view.getModel().put("gameName", gameNameStr);
			return view;
		} catch (Exception e) {
			log.error("查询游戏发生错误", e);
			return view;
		}
	}

	

	@RequestMapping("games_doUpdate")
	@ResponseBody
	public GenResult doUpdate() {
		try {
			updateAllGame.updateAllGame();
		} catch (Exception e) {
			log.error("更新游戏发生错误", e);
		}
		return null;
	}
	
	
}